package cn.btks.zuulserver.config.security;

import cn.btks.commonserver.util.sm2.SM2EncDecUtils;
import cn.btks.zuulserver.util.aes.AesEncryptUtils;
import lombok.SneakyThrows;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.net.URLDecoder;

/**
 * @Author lijun
 * @Date 2021/1/11 17:26
 */
@Component
public class Sm2PasswordEncoder implements PasswordEncoder {

    @SneakyThrows
    @Override
    public String encode(CharSequence charSequence) {
        String decrypt=null;
        try {
            //SM2进行解密
            decrypt = SM2EncDecUtils.decrypt(charSequence.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return decrypt;
    }

    @SneakyThrows
    @Override
    public boolean matches(CharSequence charSequence, String s) {
        //传过来的明文密码
        String pass1 = "" + charSequence.toString();
        pass1 = AesEncryptUtils.decrypt(pass1);
        //数据库中SM2加密后的密码
        String pass2 = encode(s);

        //判断密码是否匹配
        return pass1.equals(pass2);
    }
}
